var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthiddenSubmit:false,
    buthiddenNext: false,
    name:"",
    idCard:"",
    phone:"",
    telephone: "",
    code:"",
    verifyCodeTime:"发送验证码",
    wxType:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({ 
      wxType: options.wxType, 
      phone: options.phone.replace(/^(\d{3})\d{4}(\d+)/, "$1****$2"),
      telephone: options.phone
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  //手机号前端校验
  onPhoneEvent: function (e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ phone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ phone: e.detail.value.replace(/\D/g, '') });
    }
  },
  //手机号验证码前端校验
  onCodeEvent: function (e) {
    var that = this;
    that.setData({ code: e.detail.value.replace(/\D/g, '') });
  },

  //提交手机号
  onPhoneNext: function () {
    var that = this;
    var phone = that.data.telephone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var c = 60;
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      that.setData({ buthiddenNext: false });

      var intervalId = setInterval(function () {

        c = c - 1;
        that.setData({
          verifyCodeTime: c + 's后重发',
          buthiddenNext: true
        })
        if (c == 0) {
          clearInterval(intervalId);
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          })
        }
      }, 1000)

      wx.request({
        method: "POST",
        url: 'https://www.zaishengwang.com/wx/toObtainMessage.do',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          telephone: that.data.telephone,
        },
        success: function (res) {
          if (res.data.msg == "2") {
            wx.showModal({
              title: '该手机号不存在',
              confirmColor: '#56BDE0',
              showCancel: false,
            });
            clearInterval(intervalId);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          } else if (res.data.msg == "3") {
            wx.showModal({
              title: '今日已超过次数，请明天再尝试',
              confirmColor: '#56BDE0',
              showCancel: false,
            });
            clearInterval(intervalId);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          } else {
            that.setData({ buthiddenNext: true });
          }
        },
        fail: function (error) {
          that.setData({ buthiddenNext: true });
        }
      })
    }
  },

  bindFormSubmit: function (e) {
    var that = this;
    var name = e.detail.value.name;
    var idCard = e.detail.value.idCard; 
    var phone = that.data.telephone; 
    var code = e.detail.value.code;
    var regName = /^[\u4e00-\u9fa5]{2,4}$/;//姓名校验
    var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;//身份证校验
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;//手机号校验
    if (!name || !idCard || !phone || !code) {
      wx.showToast({
        title: '各项信息必填',
        icon: "none"
      })
      return false;
    } else if (!regName.test(name)) {
      wx.showToast({
        title: '真实姓名有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (idCard.length != 18) {
      wx.showToast({
        title: '身份证长度有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!regIdNo.test(idCard)) {
      wx.showToast({
        title: '身份证号有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    }  else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (code.length != 4) {
      // wx.showModal({
      //   title: '该地区暂不支持',
      //   confirmColor: '#56BDE0',
      //   showCancel: false,
      // })
      wx.showToast({
        title: '验证码长度有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      that.setData({ buthiddenSubmit: true });
      wx.request({
        method: "POST",
        url: 'https://www.zaishengwang.com/wx/checkPay.do',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          wxType: that.data.wxType,
          id: app.globalData.userId,//28444765309003157247057352550
          cardName: name,
          cardNum: idCard,
          telephone: phone,
          code: code,
        },
        success: function (res) {
          var c = 3;
          if (res.data.msg == "1") {
            wx.showToast({
              title: '校验成功',
              icon: 'success',
              duration: 1500
            });
            var intervalId = setInterval(function () {
              c = c - 1;
              if (c == 0) {
                clearInterval(intervalId);
                wx.navigateTo({
                  url: 'setPaymentPassword',
                })
              }
            }, 1000)
           
          } else if (res.data.msg == "2") {
            wx.showToast({
              title: '信息填写错误',
              icon: 'none',
              duration: 1500
            })
            that.setData({ buthiddenSubmit: false });
          }else if (res.data.msg == "3") {
            wx.showToast({
              title: '验证码错误',
              icon: 'none',
              duration: 1500
            })
            that.setData({ buthiddenNext: false, buthiddenSubmit: false});
          } else if (res.data.msg == "4") {
            wx.showToast({
              title: '验证码不存在',
              icon: 'none',
              duration: 1500
            })
            that.setData({ buthiddenNext: false, buthiddenSubmit: false });
          } else if (res.data.msg == "5") {
            wx.showToast({
              title: '手机号错误',
              icon: 'none',
              duration: 1500
            })
            that.setData({ buthiddenNext: false, buthiddenSubmit: false });
          } else {
            wx.showModal({
              title: '请与管理员联系',
              confirmColor: '#56BDE0',
              showCancel: false,
            })
            that.setData({ buthiddenSubmit: false });
          }
        },
        fail: function (error) {
          that.setData({ buthiddenSubmit: false });
        }
      })

    }
  }, 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})